//
//  HPPWDScreenViewController.m
//  V-POS
//
//  Created by nick liu on 12-10-29.
//  Copyright (c) 2012年 handpay. All rights reserved.
//

#import "HPPWDScreenViewController.h"

@interface HPPWDScreenViewController ()
{
    BOOL _over;
}

@end

@implementation HPPWDScreenViewController

@synthesize okButton = _okButton;

@synthesize pwdLabel = _pwdLabel;

@synthesize pwdBackButton = _pwdBackButton;

- (void) dealloc
{
    [_okButton release];
    [_pwdLabel release];
    [_pwdBackButton release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.canSave = NO;
        self.showBackButton = NO;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.navigationController setNavigationBarHidden:YES];
    
    VPOSNumberKeyBoardView *posNumberKeyBoardView = [[VPOSNumberKeyBoardView alloc] initKeyboardWithType:VPOSKeyboardType_PIN delegate:self frame:CGRectMake(0, self.view.frame.size.height - 216, 320, 216)];
    [posNumberKeyBoardView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
    [self.view addSubview:posNumberKeyBoardView];
    [posNumberKeyBoardView release];
    
    self.pwdBackButton.exclusiveTouch = self.okButton.exclusiveTouch = YES;
    
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"swipe_pwd_bg"]]];
}

- (void) viewDidUnload
{
    [self setOkButton:nil];
    [self setPwdLabel:nil];
    [self setPwdBackButton:nil];
    [super viewDidUnload];
}

- (IBAction)goBack
{
    [self.delegate inputCancel];
}

- (IBAction)btnOK
{
    if (_over)
    {
        return;
    }
    if (self.pwdLabel.text.length == 0)
    {
        [HPUIENG showAlert:@"请输入银行卡密码"];
    }
    else if (self.pwdLabel.text.length < 6)
    {
        [HPUIENG showAlert:@"请输入6位银行卡密码"];
    }
    else
    {
        _over = YES;
        [self.delegate inputOver:self.pwdLabel.text];
    }
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    self.delegate = [self.screenData objectForKey:@"delegate"];
    NSString * amount = [self.screenData objectForKey:@"amount"];
    [self showAmount:amount];
}

- (void)showAmount:(NSString *)amount
{
    line.hidden = amount.length == 0;
    amountLabel1.hidden = line.hidden;
    amountLabel2.hidden = line.hidden;
    amountLabel2.text = amount;
    _pwdBackButton.hidden = !line.hidden;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - 私有方法

- (void) reloadPwdHoldImageView
{
#define kPWDHoldImageViewStartTag       10001
    for (int i = 0; i < 6; i ++) {
        UIImageView *pwdHoldImageView = (UIImageView *)[self.view viewWithTag:(kPWDHoldImageViewStartTag + i)];
        if (i + 1 <= self.pwdLabel.text.length) {
            [pwdHoldImageView setHighlighted:YES];
        } else {
            [pwdHoldImageView setHighlighted:NO];
        }
    }
}

#pragma mark - HPPosNumberKeyBoardViewDelegate

- (void) inputView:(UIControl *)inputView notifyInputString:(NSString *)pwdString
{
    if (self.pwdLabel.text.length >= 6) {
        return;
    }
    
    [self.pwdLabel setText:[self.pwdLabel.text stringByAppendingFormat:@"%@", pwdString]];
    
    [self reloadPwdHoldImageView];
}

- (void) responseCancelButtonWithInputView:(UIControl *)inputView
{
    [self.pwdLabel setText:@""];
    [self reloadPwdHoldImageView];
}

- (void) responseCorrectButtonWithInputView:(UIControl *)inputView
{
    if (self.pwdLabel.text.length > 0) {
        [self.pwdLabel setText:[self.pwdLabel.text substringToIndex:(self.pwdLabel.text.length - 1)]];
        [self reloadPwdHoldImageView];
    }
}

- (void)responseCloseButtonWithInputView:(UIControl *)inputView
{
    [self.delegate inputCancel];
}

- (void) responseOKButtonWithInputView:(UIControl *)inputView
{
    [self.okButton sendActionsForControlEvents:UIControlEventTouchUpInside];
}

@end
